<?php
/**
 * 文章栏目控制器
 * @author caoge
 * @version 2012-5-8
 */
class ArticleAction extends BaseAction{
	public function __construct(){
		parent::__construct();
		$this->assign('hover','article');
	}
	
	/**
	 * 显示文章栏目页面
	 * 
	 * @author caoge
	 * @version 2012-5-9
	 */
	public function index(){
		//文章类别ID、文章日期
		$typeId = $this->_get("typeId","strip_tags",-1);
		$year = $this->_get("year","strip_tags",'');
		$month = $this->_get("month","strip_tags",'');
		$tagId = $this->_get("tagId","strip_tags",0);
		
		$where = 'del_flag=0';
		if ($typeId != -1){
			$where .= ' AND type_id='.$typeId;
		}
		if ($year != ''){
			$where .= " AND date_format(create_time,'%Y-%m')='".$year.'-'.$month."'";
		}
		if ($tagId != 0){
			$condition['tag_id'] = $tagId;
			$ArticleTagRelationship = D(ArticleTagRelationship);
			$idArr = $ArticleTagRelationship->where($condition)->field('article_id')->select();
			$str = '';
			foreach ($idArr as $v){
				$str .= $v['article_id'].',';
			}
			$str = trim($str,',');
			$where .= " AND article_id in (".$str.")";
		}
		
		$Article = D(Article);
		import("ORG.Util.Page");
		$count = $Article->where($where)->count();
		$p = new Page($count,6);
		
		$list = $Article->relation(true)->where($where)->limit($p->firstRow.','.$p->listRows)->order('is_top desc,sort desc,create_time desc')->select();
		//列表内容截取
		import("COM.Util.ContentSummary");
		$c = new ContentSummary();
		foreach ($list as &$v){
			$v['content'] = $c->getContentSummary($v['content']);
		}
		$this->assign('page',$p->show());
		$this->assign('list',$list);
		$this->assign('headTitle','文章');
		$this->getSideBar();
		$this->display();
	}
	
	/**
	 * 查看文章
	 * 
	 * @author caoge
	 * @version 2012-5-9
	 */
	public function view(){
		$articleId = $this->_get('articleId','',0);
		$Article = D(Article);
		$condition['article_id'] = $articleId;
		$articleInfo = $Article->relation(true)->where($condition)->find();
		if (!$articleInfo){
			$this->error('文章不存在',__APP__.'/Article/index/');
		}
		//浏览统计
		import("ORG.Util.Session");
		Session::start();
		if (!Session::get('view_'.$articleId)){
			$viewCondition = array('article_id'=>$articleId,'del_flag'=>0);
			$data['view_num'] = array('exp','view_num+1'); //表达式查询，支持SQL语法
			$Article->where($viewCondition)->save($data);
			Session::set('view_'.$articleId, 1);
		}
		
		//评论列表
		$ArticleComment = D(ArticleComment);
		import("ORG.Util.Page");
		$where = 'del_flag=0 AND status=1 AND article_id='.$articleId;
		$count = $ArticleComment->where($where)->count();
		$p = new Page($count,10);
		
		$comment = $ArticleComment->relation(true)->where($where)->limit($p->firstRow.','.$p->listRows)->order('comment_time desc')->select();
		
		$this->getSideBar();
		$this->assign('page',$p->show());
		$this->assign('articleInfo',$articleInfo);
		$this->assign('comment',$comment);
		$this->assign('headTitle',$articleInfo['title']);
		$this->display();
	}
	
	/**
	 * 评论提交
	 * 
	 * @author caoge
	 * @version 2012-5-9
	 */
	public function comment(){
		$articleId = $this->_post('articleId');
		$author = $this->_post('author','strip_tags','');
		$content = $this->_post('content','strip_tags','');
		$verifyCode = $this->_post('verifyCode');
		if(!is_numeric($articleId)){
			$this->error('文章ID不正确',__APP__.'/Article/index/');
		}
		$jumpUrl = __APP__.'/Article/view/articleId/'.$articleId;
		//检查验证码
		if(md5($verifyCode) != $_SESSION['verify']){
			$this->error('验证码错误',$jumpUrl);
		}elseif ($author == ''){
			$this->error('昵称不能为空',$jumpUrl);
		}elseif ($content == ''){
			$this->error('评论内容不能为空',$jumpUrl);
		}
		$ArticleComment = D(ArticleComment);
		$data = array(
				'article_id'=>$articleId,
				'author'=>$author,
				'content'=>$content,
				'comment_time'=>date('Y-m-d H:i:s',time()),
				'status'=>1, //先不用审核功能
				'author_IP'=>get_client_ip()
		);
		$status = $ArticleComment->data($data)->add();
		if ($status){
			//如果支持了后台审核功能，每次评论数统计放在审核通过方法里
			$Article = D(Article);
			$condition = array('article_id'=>$articleId,'del_flag'=>0);
			$commentData['comment_num'] = array('exp','comment_num+1');
			$Article->where($condition)->save($commentData);
			$this->success('评论成功',$jumpUrl);
		}else {
			$this->success('评论失败');
		}
	}
	
	/**
	 * 获取侧边栏数据
	 * 
	 * @author caoge
	 * @version 2012-7-15
	 */
	public function getSideBar(){
		$Article = D(Article);
		$ArticleType = D(ArticleType);
		$ArticleTag = D(ArticleTag);
		
		//文章分类
		$typeList = $ArticleType->categoryCount();
		//文章归档
		$dateList = $Article->dateCount();
		//阅读排行
		$viewList = $Article->getArticleByViewNum();
		//评论排行
		$commentList = $Article->getArticleByCommentNum();
		//标签云
		$tagList = $ArticleTag->where('del_flag=0')->select();
		$this->assign('typeList',$typeList);
		$this->assign('dateList',$dateList);
		$this->assign('viewList',$viewList);
		$this->assign('commentList',$commentList);
		$this->assign('tagList',$tagList);
	}
}